package rd.hardcore.ui.nav 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import rd.hardcore.engine.datas.StreamBoardManager;
	import rd.hardcore.events.NavEvent;
	import rd.hardcore.ui.pad.PadsManagerContainer;
	
	/**
	 * Navigation
	 * @author Romain Prache
	 */
	public class Nav extends Sprite
	{
		/* VAR */
		private var tabItems 	: Vector.<NavItem>;
		private var currentItem	: NavItem;
		private var oldItem		: NavItem
		private var fixLimit	: int;
		private var limit		: int;
		private var container	: Sprite;
		
		/* Nav */
		public function Nav() 
		{
			tabItems = new Vector.<NavItem>();
			container = new Sprite();
			addChild(container);
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(KeyboardEvent.KEY_UP, handleChangeItem);
						
			addEventListener(MouseEvent.CLICK, handleClick);
		}
		
		public function build( _index : int ) : void
		{
			limit = _index;
			fixLimit = _index;

			for (var i:int = 0; i < fixLimit ; i++) 
			{
				var item : NavItem = new NavItem();
				
				item.id = i;
				item.x = i * 30;
				container.addChild(item);
				
				tabItems.push(item);
			}
			
			currentItem = tabItems[0];
			currentItem.select();
			
			oldItem = currentItem;
			container.x = - container.width >> 1;

		}
		
		public function manageSelection( _id : int ) : void
		{
			if (oldItem) oldItem.unselect();
								
			currentItem = tabItems[_id];
			currentItem.select();
			
			oldItem = currentItem;
		}
		
		public function additionalBuild(_index:int):void
		{
			var i : int;
			
			for ( i = limit-1; i >= fixLimit; i--) 
			{
				if ( container.contains( tabItems[i] ) ) container.removeChild( tabItems[i] );
			}
			
			limit = fixLimit + _index;
			
			for(i = fixLimit; i < limit; i++)
			{
				if (StreamBoardManager.instance.isActiveById(i))
				{
					var item : StreamNavItem = new StreamNavItem(i);
					
					item.id = i;
					item.x = i * 30;
					container.addChild(item);
				
					tabItems[i] = item;
				}
				
			}
			container.x = - container.width >> 1;
		}
		
		private function handleChangeItem(e:KeyboardEvent):void 
		{
			//secure
			if ( PadsManagerContainer.displacing ) return;
			
			var id:int;
			var checked:Boolean = false;
			
			switch(e.keyCode) {
				case Keyboard.LEFT:
					checked = true;
					id = ((currentItem.id - 1) < 0) ? limit -1 : currentItem.id - 1;
				break;
				case Keyboard.RIGHT:
					checked = true;
					id = (currentItem.id + 1) % limit;
				break;
				default:
					if (e.keyCode < 125 && e.keyCode > 111) {
						if ((e.keyCode - 112) < limit) {
							checked = true;
							id = e.keyCode - 112;
						}else {
							checked = false;
						}
					}else{
						checked = false;
					}
			}
			if (checked && (currentItem != tabItems[id]) )
			{
				dispatchEvent(new NavEvent(NavEvent.SWITCH_PADMANAGER, id));
				
				if ( oldItem ) oldItem.unselect();
				
				currentItem = tabItems[id];
				currentItem.select();
				
				oldItem = currentItem;
			}
		}
		
		/* Click */
		private function handleClick(e:MouseEvent):void 
		{
			//secure
			if ( PadsManagerContainer.displacing || !(e.target is NavItem) ) return;
			
			if (oldItem) oldItem.unselect();
								
			currentItem = (e.target as NavItem);
			currentItem.select();
			
			dispatchEvent(new NavEvent(NavEvent.SWITCH_PADMANAGER, currentItem.id));
			
			oldItem = currentItem;
		}

	}

}